//评价
(function(){
	var db = require("../db/index");
	var config = require("../_include/config");
	var db_table_name = config.dbname.evaluation;
	var db_id_prefix = config.dbIdPrefix.evaluation;

	//构造函数
	var _con = function (info/*goods_id,star_num,comment_id*/){
		var self = this;
		//ID编号
		self._id = db_id_prefix+Math.random().toString(16).substr(2)+(+new Date).toString(16);
		//评价指数
		self.star_num = ~~info.star_num;
		if (self.star_num<0||self.star_num>10) {
			throwCusError("评价指数有误")
		}
		//评论内容
		var comment = CommentCon.get(info.comment_id);
		if (comment) {
			self.comment_id = info.comment_id;
			var commenter = comment.getCommenter();
			//设置评论为评价
			comment.setInfo({
				evaluation_id:self._id
			});
		}else{
			throwCusError("找不到指定评论");
		}

		//评价的商品
		var goods = GoodsCon.get(info.goods_id);
		if (goods) {
			self.goods_id = info.goods_id;
		}else{
			throwCusError("找不到指定商品")
		}

		db.insert(db_table_name,self);

		//
		commenter._addEvaluation(self._id);
		goods._addEvaluation(self._id);

		console.log("生成一个评价：",self._id);
	};
	//PS:模块的暴露放在require skill前面，因为二者互相依赖
	module.exports = _con;
	//技能
	_con.prototype = require("./skill");

	//静态方法
	_con.get = function(Id){
		var result = db.find_by_id(db_table_name,Id);
		if(result){
			result.__proto__ = _con.prototype;
		}
		return result;
	};

	var CommentCon = require("../comment/_con");
	var GoodsCon = require("../goods/_con");
	var AchCon = require("../ach/_con");
}());
